<template>
  <div class="app-container">
    <el-form :inline="true" class="demo-form-inline">
      <el-form-item label="房型状态">
        <el-select
          
          v-model="roomEntity.data.roomStatus"
          clearable
          placeholder="请选择"
        >
          <el-option label="可预定" value="0"> </el-option>
          <el-option label="已预定" value="1"> </el-option>
          <el-option label="下架" value="2"> </el-option>
          <el-option label="审核中" value="3"> </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="search()">查询</el-button>
      </el-form-item>
      <el-form-item>
        <el-button
          type="success"
          @click="
            dialogFormVisible = true;
            roomEntity.data = {};
          "
          >添加</el-button
        >
      </el-form-item>
    </el-form>

    <el-table :data="roomList" style="width: 100%">
      <el-table-column type="selection" prop="roomId" width="55">
      </el-table-column>
      <el-table-column prop="roomName" label="房型名称" width="180">
      </el-table-column>
      <el-table-column prop="price" label="房型价格" width="180">
      </el-table-column>
      <el-table-column prop="pic" label="房型图片" width="180">
        <template slot-scope="scope">
          <img :src="scope.row.pic" width="100px"/>
        </template>
      </el-table-column>
      <el-table-column prop="roomStatus" label="房型状态">
        <template slot-scope="scope">
          {{ scope.row.roomStatus | statusFormat }}
        </template>
      </el-table-column>
       <el-table-column prop="roomStatus" label="操作">
        <template slot-scope="scope">
          <div>
            <el-button v-if="scope.row.roomStatus != 2"
              type="primary"
              icon="el-icon-del"
              @click="
                delroom(scope.row);
              "
            >下架</el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      style="float: right"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :page-sizes="[3, 4, 5, 6]"
      :page-size="roomEntity.limit"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
    <!-- 房型的添加和修改 -->
    <el-dialog
      :title="roomEntity.data.roomId ? '修改房型信息' : '添加房型信息'"
      :visible.sync="dialogFormVisible"
    >
      <el-form :model="roomEntity">
        <el-form-item label="房型名称" >
          <el-input v-model="roomEntity.data.roomName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="房型价格" >
          <el-input v-model="roomEntity.data.price" autocomplete="off"></el-input>
        </el-form-item>
         <el-form-item label="房型类型">
        <el-select
          v-model="roomEntity.data.typeId"
          clearable
          placeholder="请选择"
        >
          <el-option label="单人间" value="1"> </el-option>
          <el-option label="双人间" value="2"> </el-option>
          <el-option label="情侣套房" value="3"> </el-option>
        </el-select>
      </el-form-item>
        <el-form-item label="房建描述" >
          
          <el-input v-model="roomEntity.data.roomDescribe" autocomplete="off"></el-input>
        </el-form-item>
            <el-form-item label="商品图片" prop="roomEntity.data.pic">
              <el-upload
                action="http://192.168.53.68:8002/homestayMerchant/room/upload"
                list-type="picture-card"
                :on-success="uploadSuccess"
                multiple="false"
                :limit="1"
              >
               <img v-if="roomEntity.data.pic" :src="roomEntity.data.pic" class="avatar">
                <i class="el-icon-plus"></i>
              </el-upload>
            </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="roomSave()"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>
<style>
.avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
<script>
import {roomSearch,roomSave,delroomById} from "@/api/room";
export default {
  data() {
    return {
      total:0,
      dialogFormVisible: false,
      roomEntity: {
        page:1,
        limit:3,
        data:{
        }
      },
      roomList: [],
    };
  },
  methods: {
    // 查询房屋状态
    search() {
      let _this = this;
      roomSearch(this.roomEntity).then(res=>{
          _this.roomList = res.data;
          _this.total = res.total;
      })
    },
    // 文件上传成功
    uploadSuccess(response, file, fileList){
      console.log(response);
      this.roomEntity.data.pic = response;
    },
    // 修改当前页面
    handleCurrentChange(val){
        this.roomEntity.page = val;
        this.search();
    },
    handleSizeChange(val){
      this.roomEntity.limit = val;
      this.search();
    },
    // 提交房型信息
    roomSave(){
        let _this = this;
        roomSave(this.roomEntity.data).then(res=>{
             this.$message({
              message: res.message,
              type: res.success ? "success" : "warning",
            });
            this.dialogFormVisible = false;
            this.roomEntity.data = {};
            _this.search();
        })
    },
    delroom(val){
      let _this = this;
       this.$confirm('你确定要下架此套房吗？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          delroomById(val.roomId).then(res=>{
           _this.$message({
              message: res.message,
              type: res.success ? "success" : "warning",
            });
        })
        })
       
    }
  },
  created() {
    this.search();
  },
  filters: {
    // 房型过滤
    statusFormat(source) {
      if (source === 0) {
        return "可预定";
      }
      if (source === 1) {
        return "已预定";
      }
      if (source === 2) {
        return "下架";
      }
      if (source === 3) {
        return "审核中";
      }
    },
  },
};
</script>